<?php
	session_start();
	$contents = '';
	function ReadUrlLink($link)
	{
	$link =	str_replace("http:","https:",$link);
	$contents = '';
	$v =$link;
	$_SESSION["linkGame"] = $v;

	$contents = '';
	
	if($link != '')	
	{


		$contents = '';

		$contents = file_get_contents($link);
		$_SESSION["content"] = $contents;
	}
		
	}
	function ReadUrl()
	{
		
	$contents = '';

	$v = $_POST['arguments']['arg1'];
	$v = str_replace("http:","https:",$v);
	$_SESSION["linkGame"] = $v;
	$contents = '';
	
	if($v != '')	
	{


		$contents = '';

		$contents = file_get_contents($v);
		$_SESSION["content"] = $contents;
	}

	
	}
//======function show screenshoot===========

	function ShowScreenShoot()
	{	
			?>
			<style> #Artwork { 
			background: url(<?php echo $_SESSION["artwork"]; ?>)no-repeat;
			height:175px;
			background-position:center;
			margin-top:5px;
			margin-bottom:10px;
}
#rev{width:245px;
	height:300px;
	background-color:#162851;
	position:relative;
	
	text-align:center;

	margin-top:5px;
	margin-bottom:15px;
	border-radius: 7px 7px 7px 7px;
	}
</style><div id='rev'><div id='Artwork'> <img alt='' src='../img/mask175_2.png' /></div><div align='center'><h2><?php echo $_SESSION["GameName"]; ?></h2></div> <div style='
	font-family:Arial, Helvetica, sans-serif;
		color:#CCC;
		font-size:13px;
		font-weight:bold;
		' align='center'>Size: <?php echo floatval($_SESSION['Size']); ?> MB</div><div class="single first"><a id="21"  href="<?php echo $_SESSION["img1"];?>" rel="lightbox[plants]">ScreenShoot</a></div>
        <div><?php echo $_SESSION["Requires"]; ?></div>
        </div>
<script type='text/javascript'>document.formUpload.txtTenGame.value = "<?php echo $_SESSION["GameName"]; ?>"; </script>
<br/>

 <div  id="aa" style="display:block;" class="imageRow">
        	<div class="set">
      
		<?php for($i=2;$i<=$_SESSION["num"];$i++)
		{
			if(!empty($_SESSION['img'.$i]))
			{
				if($i != $_SESSION["num"])
				echo'<div class="single"><a  href="'.$_SESSION["img".$i].'" rel="lightbox[plants]"></a></div>';
				else
				if($i == $_SESSION["num"])
				echo'<div class="single last"><a href="'.$_SESSION["img".$i].'" rel="lightbox[plants]" ></a></div>';
			}
		}	
		?>    </div>
        </div><?php
	}
//======function Get screenshoot===========
	function GetScreenShoot()
	{
		$contents = $_SESSION["content"];
		$mau = '/<div metrics-loc="Swoosh_".*?>([\w\W]*?)<div class="customer-reviews">/i';
			$xuat = '';

		if(preg_match($mau,$contents,$str))
		{
				$xuat = $str[0];
		}

		if(empty($xuat))
		{
			$mau = '/<div metrics-loc="Swoosh_".*?>([\w\W]*?)<div id="left-stack">/i';
			if(preg_match($mau,$contents,$str))
			{
				$xuat = $str[0];
			}
		}
			$num = strpos($xuat,'num-items=')+11;
			$num = (int)substr($xuat,$num,1);
			
			$_SESSION["num"] = $num;
			$start = strpos($xuat,'http');
			$lastE = $end = strpos($xuat,'.jpg');
			$_SESSION["img1"] = substr($xuat,$start,$end-$start+4);
			for($i=2;$i<=$num;$i++)
			{
			$start = strpos($xuat,'http',$lastE);
			$lastE = $end = strpos($xuat,'.jpg',$start);
			$_SESSION["img".$i] = substr($xuat,$start,$end-$start+4);
		
			}
			
	}
//========Get Artwork==============
	function GetArtworkAndName()
	{
		$contents = $_SESSION["content"];
		$mau = '/<img width="175" height="175".*?>([\w\W]*?)/i';
		$xuat = '';
		if(preg_match($mau,$contents,$str))
	    	{$xuat = $str[0];}
		if(empty($xuat))
		{
			echo '';
		}
		else
		{
			$start = strpos($xuat,'http');
			$end = strpos($xuat,'jpg');
			$_SESSION["artwork"] = substr($xuat,$start,$end-$start+3);  
		}
		$mau = '/<div id="title".*?>([\w\W]*?)<\/div>/i';
		$xuat = '';
		if(preg_match($mau,$contents,$str))
		{
			$xuat = $str[0];
		}
			$start = strpos($xuat,'<h1>');
			$end = strpos($xuat,'</h1>');
			$_SESSION["GameName"] = trim(substr($xuat,$start,$end-$start));
			$_SESSION["GameName"] = str_replace("<h1>","",$_SESSION["GameName"]);
			$_SESSION["GameName"] = str_replace("</h1>","",$_SESSION["GameName"]);
			//===get size game================
			$mau = '/<ul class="list"><li>([\w\W]*?)<\/ul>/i';
			$xuat = '';
		if(preg_match($mau,$contents,$str))
		{
			$xuat = $str[0];
		}
			$start = strpos($xuat,'Size:') + 13;
			$end = strpos($xuat,'MB</li>',$start);
			$sizeType = 0;
		
			if(!$end)
			{
				$end = strpos($xuat,'GB</li>',$start);
				$sizeType = 1;
			}
			$str = trim(substr($xuat,$start,$end-$start));
			if($sizeType==1)
			{
				$_SESSION["Size"] = 1000*(float)$str;
			}
			else
			$_SESSION["Size"] = (float)$str;
			
			//===get Requirements ================
			$mau = '/<p><span class="app-requirements">([\w\W]*?)<\/p>/i';
			$xuat = '';
		if(preg_match($mau,$contents,$str))
		{
			$xuat = $str[0];
		}
		
			$start = strpos($xuat,'Requirements:');
			$end = strpos($xuat,'</p>',$start);
			$str = trim(substr($xuat,$start,$end-$start));
			$_SESSION["Requires"] = "Model: ";

			if(strpos(strtolower($str),'ipod touch'))
			{
				$_SESSION["Requires"] = $_SESSION["Requires"]. " iPod touch,";
			}
			if(strpos(strtolower($str),'ipad'))
			{
				$_SESSION["Requires"] = $_SESSION["Requires"]." iPad,";
			}
			if(strpos(strtolower($str),'iphone'))
			{
				$_SESSION["Requires"] = $_SESSION["Requires"]." iPhone,";
			}
			$start = strpos($xuat,'Requires')+8;
			$end = strpos($xuat,'</p>',$start);
			$str = trim(substr($xuat,$start,$end-$start));
			$_SESSION["Requires"] = $_SESSION["Requires"]." ".$str;
			$_SESSION["Requires"] = str_replace("</span>","",$_SESSION["Requires"]);
	}
	
		if(isset($_POST["arguments"]))
		{ReadUrl();
		//==============get artwork and game name=========================
		GetArtworkAndName();
		
		//===================get screenshoot=========================	
		GetScreenShoot();
		ShowScreenShoot();
		}
		
	
			
?>
